package com.ocom.emqx.util;

import com.alibaba.nacos.client.identify.Base64;
import org.springframework.beans.factory.annotation.Value;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class EmqxApiUtil {

//    @Value("${mqtt.auth}")
//    private static String authString;

    public static String doGet(String urlStr,String authString){
        try{
            URL url = new URL(urlStr);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            conn.setDoOutput(true);

//            String authString = "admin:nSSjBwDZPTNP";
            byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
            String authStringEnc = new String(authEncBytes);
            System.out.println(authStringEnc);
            conn.setRequestProperty("Authorization", "Basic " + authStringEnc);

            conn.connect();

            int status = conn.getResponseCode();

            System.out.println(status);

            if(status == 200){
                BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String str = "";
                StringBuffer sb = new StringBuffer();
                while((str=reader.readLine()) != null){
                    sb.append(str);
                }
                return sb.toString();
            }
            System.out.println("请求emq服务失败");
        }catch(Exception e){
            e.printStackTrace();
        }
        return "error";
    }

    //下发设备标识(0x03)
    public static String  washing03(String deviceNo,Long comId){
//        03 0006 005200000001 06
        String comStr = String.format("%04d",comId.intValue());
        comStr+="00000001";
        String encDate2 = DesUtils.Cmd_Des(comStr,DesUtils.datakey);
        return "030006" +encDate2+ DesUtils.xorStr("0006"+comStr);
    }



    //消费模式 = 06 直接消费按时间结算（吹风机）
    public static String  washing0606(String deviceNo,Long comId){
//        03 0006 005200000001 06
        String comStr = String.format("%04d",comId.intValue());
        comStr+="00000001";
        String encDate2 = DesUtils.Cmd_Des(comStr,DesUtils.datakey);
        return "030006" +encDate2+ DesUtils.xorStr("0006"+comStr);
    }

    //消费模式 = 07 直接消费洗衣机（洗衣机）
    public static String  washing0607(String orderId,Long comId){
//        03 0006 005200000001 06


        //             订单号  +     模式  +  余额  +  模式 + 投币数   + 脉冲长度时间 + 脉冲间隔时间 +  时间戳 +  换算码
        Long tt = System.currentTimeMillis()/1000;

        String tyHex = DesUtils.intToHex(tt.intValue());

        String comStr=orderId + "07" + "00000000" +"02"+ "0001"+    "0064"+    "0028"     +  tyHex   + "00000000";
        String encDate2 = DesUtils.Cmd_Des(comStr,DesUtils.datakey);
        return "060018" +encDate2+ DesUtils.xorStr("0018"+comStr);
    }


    //消费模式 = 07 直接消费洗衣机（洗衣机）
    public static String  washing07(String radStr){
        //             设备编号  +     订单号  +  00 成功
        //换算码
        String hxStr  = DesUtils.Cmd_DesRsn(radStr,DesUtils.rsnkey);
        String comStr= "00000011" + hxStr ;
        String encDate2 = DesUtils.Cmd_Des(comStr,DesUtils.datakey);
        return "070008" +encDate2+ DesUtils.xorStr("0008"+comStr);
    }


    public static void main(String[]  args){

        System.out.println(washing0607("00000011",52l));

        //860013534A283B767C534A283B87A4542698A15531B7EF

        String undes = DesUtils.Cmd_UnDes("534A283B767C534A283B87A4542698A15531B7",DesUtils.datakey);
        System.out.println(undes);

        //0000000000000000000008249B0C0666DA7190

        System.out.println(washing07(undes.substring(22,30)));


        //87000B534A283B767C534A28437603

        String undes2 = DesUtils.Cmd_UnDes("534A283B767C534A284376",DesUtils.datakey);
        System.out.println(undes2);
    }


}
